package com.keith.asteria.serviceapi.systemApi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import system.SysUser;
import system.mapper.SysUserMapper;
import utils.ResponseUtil;

/**
 * @ClassName SysUserApi
 * @Author chenli
 * @Time 2018/10/31
 **/
@RestController
public class SysUserApi {

	private final SysUserMapper sysUserMapper;

	@Autowired
	public SysUserApi(SysUserMapper sysUserMapper) {
		this.sysUserMapper = sysUserMapper;
	}

	@RequestMapping("/findUser")
	public Object findUser(@RequestParam(name = "id") Integer id) {
		return sysUserMapper.findById(id);
	}


	@PostMapping("/userLogin")
	public Object userLogin(@RequestBody SysUser user) {
		String username = user.getUsername();
		String password = user.getPassword();
		SysUser sysUser = sysUserMapper.findUserByUsernameAndPassword(username, password);
		if (sysUser != null) {
			return ResponseUtil.returnResp(200, "登录成功！");
		} else {
			return ResponseUtil.returnResp(400, "登录失败！");
		}
	}

	@PostMapping("/userSave")
	public Object userSave(@RequestBody SysUser user) {
		SysUser sysUser = findUserByName(user);
		if (sysUser != null) {
			return ResponseUtil.returnResp(400, "用户已存在！");
		}
		sysUserMapper.insertUser(user);
		return ResponseUtil.returnResp(200, "保存成功！");
	}

	@GetMapping("/findUserByName")
	public SysUser findUserByName(@RequestBody SysUser user) {
		return sysUserMapper.findUserByName(user);
	}
}
